<?php

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * Description of annonce_m
    *
    * @author Gidaff_01
    */
    class annonce_m extends Model {

        //put your code here
        function Annonce_m() {
            parent::Model();
            $this->load->database();
        }

        function get_day_allow_by_level($level) {
            $this->db->select('day_allow');
            $this->db->from('animus_anounce_level');
            $this->db->where('id', $level);
            $this->db->order_by('id DESC');
            return $this->db->get();
        }

        function ajouter_annonce($data) {
            $query = $this->db->insert('animus_annonce', $data);
            return $query;
        }

        function search_annonce($region_id, $per_page, $off_set) {
            $region_id = mysql_escape_string($region_id);
            $this->db->select('animus_annonce.*,animus_sub_region.name as sub_region');
            $this->db->from('animus_annonce,animus_sub_region');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.sub_region IN (SELECT animus_sub_region.id FROM animus_sub_region WHERE animus_sub_region.region_id=' . $region_id . ')');
            $this->db->where('animus_annonce.active', '1');
            $this->db->where('animus_annonce.date_expried >'.  time());
            $this->db->order_by('id DESC');
            $this->db->limit($per_page, $off_set);
            return $this->db->get()->result_array();
        }

        function count_search_annonce($region_id) {
            $region_id = mysql_escape_string($region_id);
            $this->db->select('animus_annonce.*,animus_sub_region.name as sub_region');
            $this->db->from('animus_annonce,animus_sub_region');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.sub_region IN (SELECT animus_sub_region.id FROM animus_sub_region WHERE animus_sub_region.region_id=' . $region_id . ')');
            $this->db->where('animus_annonce.active', '1');
            $this->db->where('animus_annonce.date_expried >'.  time());
            $this->db->order_by('id DESC');
            return count($this->db->get()->result_array());
        }

        function get_annonce_detail($id) {
            $this->db->select('animus_annonce.*,animus_sub_region.name as sub_region,lehu_users.email,
            lehu_profiles.display_name,lehu_profiles.first_name,lehu_profiles.last_name,
            lehu_profiles.phone,lehu_profiles.mobile,lehu_profiles.address
            ');
            $this->db->from('animus_annonce,animus_sub_region,lehu_profiles,lehu_users');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.posted_by = lehu_profiles.user_id');
            $this->db->where('animus_annonce.posted_by = lehu_users.id');
            $this->db->where('animus_annonce.id', $id);
            return $this->db->get()->result_array();
        }

        function search_annonce_adv($title, $region_id, $per_page, $off_set) {
            if ($title == '_')
                $title = '';
            if ($region_id == '_')
                $region_id = '';
            $region_id = mysql_escape_string($region_id);
            $this->db->select('animus_annonce.*,animus_sub_region.name as sub_region');
            $this->db->from('animus_annonce,animus_sub_region');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.active', '1');
            $this->db->where('animus_annonce.date_expried >'.  time());
            if ($title != '') {
                $this->db->like('animus_annonce.title', $title);
            }
            if ($region_id != '') {
                $this->db->where('animus_annonce.sub_region IN (SELECT animus_sub_region.id FROM animus_sub_region WHERE animus_sub_region.region_id=' . $region_id . ')');
            }
            $this->db->order_by('id DESC');
            $this->db->limit($per_page, $off_set);
            return $this->db->get()->result_array();
        }

        function count_search_annonce_adv($title, $region_id) {
            if ($title == '_')
                $title = '';
            if ($region_id == '_')
                $region_id = '';
            $region_id = mysql_escape_string($region_id);
            $title = mysql_escape_string($title);
            $this->db->select('animus_annonce.*,animus_sub_region.name as sub_region');
            $this->db->from('animus_annonce,animus_sub_region');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.active', '1');
            $this->db->where('animus_annonce.date_expried >'.  time());
            if ($title != '') {
                $this->db->like('animus_annonce.title', $title);
            }
            if ($region_id != '') {
                $this->db->where('animus_annonce.sub_region IN (SELECT animus_sub_region.id FROM animus_sub_region WHERE animus_sub_region.region_id=' . $region_id . ')');
            }
            $this->db->order_by('id DESC');
            return count($this->db->get()->result_array());
        }
        function get_annonce($per_page, $off_set) {
            $this->db->select('animus_annonce.*,animus_sub_region.name as sub_region');
            $this->db->from('animus_annonce,animus_sub_region');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.date_expried >'.time() );  
            $this->db->order_by('id DESC');
            $this->db->limit($per_page, $off_set);
            return $this->db->get()->result_array();
        }
        function count_annonce(){
            $this->db->select('animus_annonce.*,animus_sub_region.name as sub_region');
            $this->db->from('animus_annonce,animus_sub_region');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.active = 1');
            $this->db->where('animus_annonce.date_expried >'.time() );
            $this->db->order_by('id DESC');
            return count($this->db->get()->result_array());
        }
    }

?>
